package com.example.network_services_twit_begin;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.view.View;
import android.widget.EditText;

public class SearchTask extends AsyncTask<String, Void, String> {

	EditText etTwitterResponse;
	
	String firstHalfOfUrl = "https://api.twitter.com/1.1/search/tweets.json?q=";
	String completeSearchUrl;
	
	public SearchTask(EditText etTwitterResponse)
	{
		this.etTwitterResponse = etTwitterResponse;
	}
	
	@Override
	protected String doInBackground(String... params) {
		// TODO Auto-generated method stub
		
		completeSearchUrl = firstHalfOfUrl + params[0];
		
		HttpClient client = new DefaultHttpClient();	
		HttpGet httpGet = new HttpGet(completeSearchUrl);	
		ResponseHandler<String> handler = new BasicResponseHandler();
		
		String result = null;
		
		try {
			AuthorisationManager.signWithBearerToken(httpGet);
			result = client.execute(httpGet, handler);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return result;
	}

	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		this.etTwitterResponse.setText(result);
		//this.etTwitterResponse.setText(completeSearchUrl);
	}
	
}
